# -*- coding: utf-8 -*-
# Houdini To Arnold
# Copyright (C) 2013 Erkan Ozgur Yilmaz
#
# This file is part of Houdini To Arnold.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation;
# version 2.1 of the License.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

import re
import hou

#    template="""polymesh
#{
# name %(name)s
# nsides %(prim_count) 1 UINT
#  %(prim_vertex_counts)
# vidxs %(vertex_count) 1 UINT
#  %(prim_vertex_ids)
# nidxs 24 1 UINT
#  %(point_normal_ids)
# uvidxs 24 1 UINT 
#  %(vertex_uv_ids)
# vlist %(point_count) 1 POINT
#  %(point_positions)
# nlist 24 1 VECTOR
#  %(point_normals)
# uvlist 14 1 POINT2
#  %(vertex_uvs)
# smoothing on
# visibility 65535
# sidedness 65535
# receive_shadows on
# self_shadows on
# matrix
#  1 0 0 0
#  0 1 0 0
#  0 0 1 0
#  0 0 0 1
# shader "initialShadingGroup"
# opaque on
# sss_sample_spacing 0.100000001
# sss_sample_distribution "blue_noise"
# id 683108022
# declare mtoa_shading_groups constant ARRAY NODE
# mtoa_shading_groups "initialShadingGroup"
#}
#"""


def new_line_every_nth_char(data, n):
    return re.sub("(.{%i})" % n, "\\1\n", data, 0)


def export_geometry(path):
    """returns geo in ass format
    """

    template = """polymesh
{
 name %(name)s
 nsides %(prim_count)i 1 UINT
  %(prim_vertex_counts)s
 vidxs %(vertex_count)i 1 UINT
  %(prim_vertex_ids)s
 vlist %(point_count)i 1 POINT
  %(point_positions)s
 smoothing on
 visibility 65535
 sidedness 65535
 receive_shadows on
 self_shadows on
 matrix
  1 0 0 0
  0 1 0 0
  0 0 1 0
  0 0 0 1
 shader "aiStandard2SG"
 opaque on
 sss_sample_spacing 0.100000001
 sss_sample_distribution "blue_noise"
 id 683108022
 declare mtoa_shading_groups constant ARRAY NODE
 mtoa_shading_groups "aiStandard2SG"
}

"""
    if not path:
        return ''

    params = {}

    node = hou.node(path)
    render_node = node.renderNode()
    params['name'] = render_node.path().replace('/', '_')

    geo = render_node.geometry()
    assert isinstance(geo, hou.Geometry)

    prim_count = geo.intrinsicValue('primitivecount')
    params['prim_count'] = prim_count

    prim_vertex_counts = []
    prim_vertex_ids = []
    for prim in geo.prims():
        prim_vertex_counts.append(`prim.intrinsicValue('vertexcount')`)
        for vertex in prim.vertices():
            prim_vertex_ids.append(`vertex.point().number()`)

    # join data to string
    params['prim_vertex_counts'] = ' '.join(prim_vertex_counts)
    params['prim_vertex_ids'] = ' '.join(prim_vertex_ids)

    params['vertex_count'] = len(prim_vertex_ids)

    point_positions = geo.pointFloatAttribValues('P')
    params['point_positions'] = ' '.join(map(str, list(point_positions)))

    params['point_count'] = geo.intrinsicValue('pointcount')

    return template % params
